package com.authine.cloudpivot.util;

import cn.hutool.core.lang.UUID;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;

public class OssUtil {


    private static final String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

    private static final String bucketName = "yxyunxiaos";

    private static final String accessKeyId = "LTAI5tCt7eFp42bXYZLZsqF8";

    private static final String accessKeySecret = "dW44QQE39QUnhdRPLXAverOYAwlJ1u";


    /**
     * 更新文件名
     */
    public static void UpdateFileName(String oldName,String newName)  {
        DefaultCredentialProvider credentialsProvider = null;
        credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider(accessKeyId,accessKeySecret);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // 将examplebucket下的srcobject.txt拷贝至同一Bucket下的destobject.txt。
            ossClient.copyObject(bucketName, oldName, bucketName, newName);

            // 删除srcobject.txt。
            //ossClient.deleteObject(bucketName, oldName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static String copyFile(String refId)  {
        DefaultCredentialProvider credentialsProvider = null;
        credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider(accessKeyId,accessKeySecret);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            String newName = UUID.fastUUID().toString();
            String newFilename ="";
            int dotIndex = refId.lastIndexOf('.');
            if (dotIndex != -1) {
                String extension = refId.substring(dotIndex);
                newFilename = newName + extension;
            }
            // 将examplebucket下的srcobject.txt拷贝至同一Bucket下的destobject.txt。
            ossClient.copyObject(bucketName, refId, bucketName, newFilename);
            return newFilename;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }


}
